/*
 * Copyright (c) 2010 Scott Morgan
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package captaintest.emma;

import captaintest.Coverage;
import captaintest.FileCoverage;
import captaintest.util.Assert;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class EmmaParser {

    List<FileCoverage> coverageList = new ArrayList<FileCoverage>();

    public EmmaParser(File sourceFile) {
        Assert.notNull(sourceFile);
        Assert.isTrue(sourceFile.exists());

        try {
            parse(sourceFile);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


    }

    public List<FileCoverage> getCoverageList() {
        return coverageList;
    }

    public static void main(String[] args) {
        new EmmaParser(new File("./core/data/emma/example.xml"));
    }

    private void parse(File sourceFile) throws Exception {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        SaxHandler handler = new SaxHandler();
        parser.parse(sourceFile, handler);

        coverageList = handler.getFileCoverageList();
    }

    private static class SaxHandler extends DefaultHandler {

        private String currentPackage;
        private String currentSourceFile;
        private FileCoverage currentCoverage;
        private List<FileCoverage> fileCoverageList = new ArrayList<FileCoverage>();

        public List<FileCoverage> getFileCoverageList() {
            return fileCoverageList;
        }

        public void startElement(String uri, String localName,
                                 String qName, Attributes attrs) throws SAXException {

            if ("package".contains(qName)) {
                currentPackage = attrs.getValue("name");
            } else if ("srcfile".equals(qName)) {
                currentSourceFile = attrs.getValue("name");
                currentCoverage = new FileCoverage(currentPackage + "." + currentSourceFile);
            } else if ("class".equals(qName)) {
                currentSourceFile = null;
            } else if ("coverage".equals(qName)) {
                if (currentSourceFile != null) {
                    String typeAttr = attrs.getValue("type");
                    String type = typeAttr.substring(0, typeAttr.indexOf(','));
                    String valueAttr = attrs.getValue("value");
                    String lineAttr = valueAttr.substring(valueAttr.indexOf('(') + 1, valueAttr.indexOf(')'));
                    String[] lineValues = lineAttr.split("/");
                    Coverage coverage = new Coverage(type, Double.parseDouble(lineValues[0]), Integer.parseInt(lineValues[1]));
                    currentCoverage.addCoverage(coverage);
                }
            } else {
            }
        }

        public void endElement(String uri, String localName, String qName) throws SAXException {

            if ("srcfile".equals(qName)) {
                if (currentCoverage != null) {
                    fileCoverageList.add(currentCoverage);
                }
            }
        }
    }

}
